<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 *
 * 创建时间：2021-12-09 23:17:08
 *
 * 项目：/qq  -  $  - qqModelHelper.php
 *
 * 作者：{{AUTO GENERATE}}
 */

//此文件使用程序自动生成，下次生成时【不会】覆盖。
//在这里可以补充、完善你的程序逻辑

namespace modules\qq\table\qq;

use Lev;

!defined('INLEV') && exit('Access Denied LEV');

class BaseQqModelMiddle extends BaseQqModel
{

    public static function saveForm($formPre = 'datax', $route = '', $form = null, $upData = null, $upDataForce = null, $opInfo = null)
    {
        return parent::saveForm($formPre, $route, $form, $upData, $upDataForce, $opInfo); // TODO: Change the autogenerated stub
    }

    /**
     * 额外的表单字段，不会存入数据库
     * @return array
     */
    public static function extInputs()
    {
        return parent::extInputs(); // TODO: Change the autogenerated stub
    }

    public static function headerHtm()
    {
        //return parent::headerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义headerHtm：文件位置：'.__DIR__ . '/qqModelHelper.php'.'</tips>';
    }

    public static function footerHtm()
    {
        //return parent::footerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerHtm</tips>';
    }

    public static function footerFormInnerHtm()
    {
        //return parent::footerFormInnerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerFormInnerHtm</tips>';
    }

    public static function sex() {
        return [
            '女',
            '男'
        ];
    }

    public static function qqstatus() {
        return [
            0 => '未验证',
            1 => '未登陆',
            2 => '成功登陆',
            3 => '登陆失败',
            4 => '掉线',
            5 => '在线'
        ];
    }

    public static function qqstatusCheckHtm($qqstatus, $uptime, $online = null) {
        return $online || static::qqstatusCheck($qqstatus, $uptime) ? '<absxn>在线</absxn>' : '<absxg>掉线</absxg>';
    }

    /**
     * 检查QQ号是否在线，在线返回true
     * @param $qqstatus
     * @param $uptime
     * @return bool
     */
    public static function qqstatusCheck($qqstatus, $uptime) {
        if (in_array($qqstatus, [2, 5])) {
            if (Lev::$app['timestamp'] < $uptime + 3600 * 12) {
                return true;
            }
        }
        return false;
    }

    public static function saveQQ($qq,  $pwd = '', $loginRes = '', $qqnick = '', $qqstatus = 1)
    {
        if (!is_numeric($qq)) {
            return;
        }
        $uid = Lev::$app['uid'];
        $ck = static::findOne(['qq'=>$qq, 'uid'=>$uid]);
        $data = [
            'uid'      => $uid,
            'qq'       => $qq,
            'qqstatus' => $qqstatus,
            'uptime'   => Lev::$app['timestamp'],
        ];
        $md5pwd = static::md5QQpwd($pwd);
        $data['loginmsg'] = $loginRes.' # '.$md5pwd;
        $qqnick && $data['qqnick'] = $qqnick;
        if ($ck) {
            $pwd && $qqnick && $data['qqpwd'] = $md5pwd;
            static::update($data, ['id'=>$ck['id']]);
        }else {
            $data['qqpwd'] = $md5pwd;
            $data['addtime'] = Lev::$app['timestamp'];
            static::insert($data);
        }
    }

    public static function ckQQpwd($pwd)
    {
        if (substr($pwd, -2) == '@@') {
            $pwd = static::md5QQpwd(substr($pwd, 0, -2), true) ?: $pwd;
        }
        return $pwd;
    }

    public static function md5QQpwd($pwd, $un = false) {
        return Lev::authcodev($pwd, $un, '123123qweqwe');
    }
}


class qqModelHelper extends BaseQqModelMiddle {

    public static function allColumns()
    {
        return parent::allColumns(); // TODO: Change the autogenerated stub
    }

    public static function safeColumnsInpus($columns)
    {
        return parent::safeColumnsInpus($columns); // TODO: Change the autogenerated stub
    }

    public static function setFormSettings($settings, $result = [])
    {
        return parent::setFormSettings($settings, $result); // TODO: Change the autogenerated stub
    }

    public static function saveInputs($datas, $where = [], $inId = false, $tables = [], $inputs = [], $formSet = true, $tablesFormData = [])
    {
        return parent::saveInputs($datas, $where, $inId, $tables, $inputs, $formSet, $tablesFormData); // TODO: Change the autogenerated stub
    }

    public static function getNextSetup()
    {
        return parent::getNextSetup(); // TODO: Change the autogenerated stub
    }

    public static function inputsSetup()
    {
        return parent::inputsSetup(); // TODO: Change the autogenerated stub
    }

    public static function setupDesc()
    {
        return parent::setupDesc(); // TODO: Change the autogenerated stub
    }

}